/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.servlet;

import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import linkeddata.arida.ufc.br.VCD.ElementsVCDS;
import linkeddata.arida.ufc.br.VCD.ExecuteQueryVCDS;
import linkeddata.arida.ufc.br.VCD.ExtractsContentVCD;
import linkeddata.arida.ufc.br.search.FileHierarchyVCDS;

/**
 *
 * @author jeovane
 */
//@WebServlet(name = "ExecuteQueryJenaVCD", value = "/execute-vcd")
public class ExecuteQueryJenaVCDServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] vcds = request.getParameterValues("vcdss[]");
        String valueClick = request.getParameter("valueClick");
        String tableID = request.getParameter("tableID");
        int countClick = Integer.parseInt(request.getParameter("countClick"));
        String jsonTextQuery = request.getParameter("jsonTextQuery");
        String resultQuery = null;
        List<String> varsQuery = new ArrayList<String>();
        List<String> extractVCD = new ArrayList<String>();
        Map<String, ArrayList<String>> mapServices = new HashMap<String, ArrayList<String>>();

        if ("".equals(jsonTextQuery) || jsonTextQuery == null) {
            jsonTextQuery = "{}";
        }

        ElementsVCDS elementsVCDS = new ElementsVCDS();
        FileHierarchyVCDS fhv = new FileHierarchyVCDS(elementsVCDS);

        for (int i = 0; i < vcds.length; i++) {
            ExtractsContentVCD e = new ExtractsContentVCD(new File(vcds[i]));
            elementsVCDS.setExtractList(e);
        }

        ExecuteQueryVCDS eq = new ExecuteQueryVCDS(elementsVCDS, fhv, jsonTextQuery);

        if (tableID == null) {
            tableID = "";
        }

        try {
//            if (ElementsVCDS.getExistingKey().containsKey(valueClick)) {
//                resultQuery = ElementsVCDS.getExistingKey().get(valueClick);
//            } else {
//                resultQuery = eq.executeQueryVCD(valueClick, countClick);
//                eq.queriesAlreadyExecuted(valueClick, resultQuery, countClick);
//            }
            varsQuery = eq.pathBetweenVCDS(countClick);
            extractVCD = eq.extractDestinationVCD(countClick);
            mapServices = eq.getServices(countClick);
            resultQuery = eq.executeQueryVCD(valueClick, countClick);
        } catch (Exception ex) {
            System.out.println("ERROR Servlet: " + ex);
        }

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        String bothJson = "[" + resultQuery + "," + new Gson().toJson(extractVCD) + "," + new Gson().toJson(varsQuery) + "," + new Gson().toJson(mapServices) + "]";
        response.getWriter().write(bothJson);

    }
}
